package com.store.bookstore_backend.service.impl;

import com.store.bookstore_backend.Util.JwtUtil;
import com.store.bookstore_backend.domain.User;
import com.store.bookstore_backend.dto.*;
import com.store.bookstore_backend.exception.BookStoreException;
import com.store.bookstore_backend.mapper.UserMapper;
import com.store.bookstore_backend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public void userRegister(UserRegisterDTO userRegisterDTO) {
        if (userMapper.getUserByUsername(userRegisterDTO.getUsername()) != null) {
            //如果用户名已经存在就抛异常
            throw new BookStoreException(BookStoreException.CodeMsgEnum.USER_USERNAME_IS_EXIST);
        }
        if (userRegisterDTO.getUsername() == null || userRegisterDTO.getUsername().trim().isEmpty()) {
            //如果传入的参数用户名不存在
            throw new BookStoreException(BookStoreException.CodeMsgEnum.USER_USERNAME_IS_NOT_NULL);
        }
        if (userRegisterDTO.getPassword() == null || userRegisterDTO.getPassword().trim().isEmpty()) {
            //如果传入的参数密码不存在
            throw new BookStoreException(BookStoreException.CodeMsgEnum.USER_PASSWORD_IS_NOT_NULL);
        }

        User user = new User();
        user.setUsername(userRegisterDTO.getUsername());
        user.setPassword(userRegisterDTO.getPassword());
        user.setBirthday(userRegisterDTO.getBirthday());
        user.setGender(userRegisterDTO.getGender());
        user.setPhone(userRegisterDTO.getPhone());
        user.setAvatar(userRegisterDTO.getAvatar());
        userMapper.setUser(user);
        System.out.println("User registered: " + userRegisterDTO.getUsername());
    }

    @Override
    public LoginDataDTO userLogin(UserLoginDTO userLoginDTO) {
        User user = userMapper.getUserByUsername(userLoginDTO.getUsername());
        if (user == null) {
            throw new BookStoreException(BookStoreException.CodeMsgEnum.USER_IS_NULL);
        }
        if (user.getStatus() == 1) {
            //如果status为1则用户被封禁账户无法登录
            throw new BookStoreException(BookStoreException.CodeMsgEnum.USER_STATUS_CLOSE);
        }
        if (!user.getPassword().equals(userLoginDTO.getPassword())) {
            throw new BookStoreException(BookStoreException.CodeMsgEnum.PASSWORD_ERROR);
        }
        Long userId = user.getId();
        String token = JwtUtil.generateToken(userId);
        return new LoginDataDTO(token, user);
    }

    @Override
    public User getUserInfo(HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        // 根据用户ID从数据库中获取用户信息
        User user = userMapper.getUserById(userId);
        if (user == null) {
            //解析后用户不存在
            throw new BookStoreException(BookStoreException.CodeMsgEnum.USER_IS_NULL);
        }
        return user;
    }

    @Override
    public void UpdatePassWord(HttpServletRequest request, UserUpdatePasswordDTO userUpdatePasswordDTO) {
        // 从Request中解析用户ID
        Long userId = (Long) request.getAttribute("userId");

        // 根据用户ID从数据库中获取用户原密码
        String oldPassword = userMapper.getPasswordById(userId);

        if (oldPassword == null) {
            throw new BookStoreException(BookStoreException.CodeMsgEnum.PASSWORD_ERROR);
        }
        if (!oldPassword.equals(userUpdatePasswordDTO.getOldPassword())) {
            throw new BookStoreException(BookStoreException.CodeMsgEnum.PASSWORD_ERROR);
        }
        User user = new User();
        user.setId(userId);
        user.setPassword(userUpdatePasswordDTO.getNewPassword());
        userMapper.updateUser(user);
    }

    @Override
    public void UpdateUser(UserMessageUpdateDTO userMessageUpdateDTO) {
        User user = new User();
        user.setUsername(userMessageUpdateDTO.getUsername());
        user.setId(userMessageUpdateDTO.getId());
        user.setGender(userMessageUpdateDTO.getGender());
        user.setPhone(userMessageUpdateDTO.getPhone());
        user.setAvatar(userMessageUpdateDTO.getAvatar());
        user.setBirthday(userMessageUpdateDTO.getBirthday());
        userMapper.updateUser(user);
    }

}

